#include "Lexer.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool inputFromFile(char* filename) {
    file = fopen(filename, "r");
    if (file == NULL) {
        printf ("Error opening file\n");
        return false;
    }
    return true;
}

Token nextToken() {
    char c;

    Token catchIdentifier(char, FILE*);
    Token catchNumber(char, FILE*);
    Token catchOperator(char, FILE*);

    while(!feof(file)) {
        c = fgetc(file);
        if (c == ' ' || c == '\t' || c == '\n') {
            continue;
        } else if (c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z')) {
            return catchIdentifier(c, file);
        } else if (c >= '0' && c <= '9') {
            return catchNumber(c, file);
        } else {
            return catchOperator(c, file);
        }
    }
}

Token catchIdentifier(char c, FILE* p_file) {}

Token catchNumber(char c, FILE* p_file) {
    Token token;
    fpos_t file_pos;

    char buf[TOKEN_BUFFER_SIZE + 1]; // Last slot for \0
    int buf_index = 0;

    do {
        if (buf_index != 0) {
            fgetpos(p_file, &file_pos);
            c = fgetc(p_file);
            if (c < '0' && c > '9') {
                fsetpos(p_file, &file_pos);
                break;
            }
        }
        buf[buf_index] = c;
        buf_index++;
    } while(!feof(p_file) && buf_index < TOKEN_BUFFER_SIZE);

    return token;
}

Token catchOperator(char c, FILE* p_file) {
    Token token;
    fpos_t  file_pos;
    fgetpos(p_file, &file_pos);

    switch (c) {
        case ';':
            token.type = SC;
            break;

        case ',':
            token.type = COMMA;
            break;

        case '(':
            token.type = LP;
            break;

        case ')':
            token.type = RP;
            break;

        case '+':
            token.type = PLUS;
            break;

        case '-':
            token.type = MINUS;
            break;

        case '*':
            token.type = MUL;
            break;

        case '/':
            token.type = DIV;
            break;

        case '&':
            if(!feof(p_file)) c = fgetc(p_file);
            if(c == '&') {
                token.type = AND;
                break;
            } else {
                token.type = ERROR;
                fsetpos(p_file, &file_pos);
                break;
            }

        case '|':
            if(!feof(p_file)) c = fgetc(p_file);
            if(c == '|') {
                token.type = OR;
                break;
            } else {
                token.type = ERROR;
                fsetpos(p_file, &file_pos);
                break;
            }

        case '!':
            token.type = NOT;
            break;

        case '>':
            if(!feof(p_file)) c = fgetc(p_file);
            if(c == '=') {
                token.type = GE;
                break;
            } else {
                token.type = GT;
                fsetpos(p_file, &file_pos);
                break;
            }

        case '<':
            if(!feof(p_file)) c = fgetc(p_file);
            if(c == '=') {
                token.type = LE;
                break;
            } else {
                token.type = LT;
                fsetpos(p_file, &file_pos);
                break;
            }

        case '=':
            if(!feof(p_file)) c = fgetc(p_file);
            if(c == '=') {
                token.type = EQ;
                break;
            } else {
                token.type = ASSIGN;
                fsetpos(p_file, &file_pos);
                break;
            }

        default:
            token.type = ERROR;
    }
    return token;
}

